package org.game.support;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import org.game.constant.APIConstant;
import org.game.util.StringUtil;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public abstract class TopStatusBarController implements Initializable {
    @FXML
    protected BorderPane topBar;

    @FXML
    protected Label timer;

    @FXML
    protected ImageView reset;

    protected volatile boolean isStop = false;

    /**
     * 初始化 主面板
     */
    protected abstract void initMainPane();

    /**
     * 重启事件用户自定义方法
     */
    protected abstract void resetMouseClick();

    protected void initTopBar() {
        topBar.setPrefWidth((Integer) APIConstant.CONFIG.get("width") * (Integer) APIConstant.CONFIG.get("cellWidth"));
        topBar.setPrefHeight(100);
        topBar.setPadding(new Insets(10, 10, 10, 10));
        reset.setImage(new Image("/image/smiley.png"));

        initTimer();
    }

    protected void initTimer() {
        timer.setText("00:00");
        isStop = false;

        APIConstant.TIME_SERVICE.scheduleAtFixedRate(() -> {
            while (isStop) {
            }

            String timeFormat = StringUtil.getFormatTime(APIConstant.START_TIME.getAndIncrement());

            Platform.runLater(() -> {
                timer.setText(timeFormat);
            });
        }, 0, 1, TimeUnit.SECONDS);
    }

    @FXML
    public void resetMouseClick(MouseEvent event) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                resetMouseClick();
                reset.setImage(new Image("/image/smiley.png"));
                APIConstant.START_TIME.set(0);
                isStop = false;
            }
        });
    }
}
